package cn.edu.zafu.bigdata.x.hjn;

import java.util.Scanner;

public class StudentManager {
    private Scanner scanner;

    public StudentManager() {
        this.scanner = new Scanner(System.in);
    }

    // 显示主菜单
    public void showMainMenu() {
        while (true) {
            System.out.println("===== 学生信息管理系统 =====");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");

            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    addStudentInfo();
                    break;
                case "2":
                    searchStudentInfo();
                    break;
                case "3":
                    updateStudentInfo();
                    break;
                case "4":
                    deleteStudentInfo();
                    break;
                case "5":
                    System.out.println("程序退出");
                    return;
                default:
                    System.out.println("无效输入，请重新选择！");
            }
        }
    }

    // 添加学生信息
    private void addStudentInfo() {
        System.out.println("请输入学生学号：");
        String id = scanner.nextLine();
        System.out.println("请输入学生姓名：");
        String name = scanner.nextLine();
        System.out.println("请输入学生电话：");
        String phone = scanner.nextLine();

        Student student = new Student(id, name, phone);
        if (DatabaseHelper.getStudentById(id) != null) {
            System.out.println("学号重复，请重新输入！");
        } else if (DatabaseHelper.addStudent(student)) {
            System.out.println("学生信息添加成功！");
        } else {
            System.out.println("学生信息添加失败！");
        }
    }

    // 检索学生信息
    private void searchStudentInfo() {
        while (true) {
            System.out.println("请输入学号进行检索，输入 'QUIT' 退出：");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                return;
            }

            Student student = DatabaseHelper.getStudentById(id);
            if (student != null) {
                System.out.println("学生信息: " + student);
            } else {
                System.out.println("未找到该学生！");
            }
        }
    }

    // 更新学生信息
    private void updateStudentInfo() {
        while (true) {
            System.out.println("请输入学号进行更新，输入 'QUIT' 退出：");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                return;
            }

            Student student = DatabaseHelper.getStudentById(id);
            if (student != null) {
                System.out.println("当前学生信息: " + student);
                System.out.println("请输入新的姓名：");
                String name = scanner.nextLine();
                System.out.println("请输入新的电话：");
                String phone = scanner.nextLine();
                student = new Student(id, name, phone);
                if (DatabaseHelper.updateStudent(student)) {
                    System.out.println("学生信息更新成功！");
                } else {
                    System.out.println("学生信息更新失败！");
                }
            } else {
                System.out.println("未找到该学生！");
            }
        }
    }

    // 删除学生信息
    private void deleteStudentInfo() {
        while (true) {
            System.out.println("请输入学号进行删除，输入 'QUIT' 退出：");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                return;
            }

            if (DatabaseHelper.deleteStudent(id)) {
                System.out.println("学生信息删除成功！");
            } else {
                System.out.println("未找到该学生！");
            }
        }
    }
}

